package control;


import Entity.messageBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class messageControl {
    public messageControl() {
    }
    //获取所有留言信息
    public static void get_message(HttpServletRequest request, HttpServletResponse response)
    {
        try{
            Connection con=null;
            Statement stmt=null;
            ResultSet rs=null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url="jdbc:h2:~/test1";
            //System.out.println("到达");
            con= DriverManager.getConnection(url,"root","root");
            //System.out.println("到达2");
            HttpSession session=request.getSession();
            stmt=con.createStatement();
            String sql="select * from good_message";
            rs=stmt.executeQuery(sql);
            ArrayList messagelist=null;
            if((ArrayList)session.getAttribute("messagelist")==null){
                messagelist=new ArrayList();
                while(rs.next()){
                    messageBean ff = new messageBean();
                    ff.setMessage_id(rs.getString("message_id"));
                    ff.setUse_id(rs.getString("use_id"));
                    ff.setOrder_id(rs.getString("order_id"));
                    ff.setWhat(rs.getString("what"));
                    ff.setSo(rs.getString("so"));
                    messagelist.add(ff);
                }
                System.out.println(messagelist);
                session.setAttribute("messagelist", messagelist);
            }
            rs.close();
            stmt.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
